/*Elise Eiden
Taryn Green
Kevin McGinn 

Software Engineering
Network Feasibility Test

Referencing the Boost Daytime example

A simple client. It sends keyboard input to the server to update the cube's coordinates. It then receives the updated coordinates and prints them. Inputting a space will end the program.*/

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <hostname>" << std::endl;
      return 1;
    }

    // Create io service
    boost::asio::io_service io_service;

    // Create a resolver to find the correct remote endpoint
    udp::resolver resolver(io_service);
    udp::resolver::query query(udp::v4(), argv[1], "daytime");
    udp::endpoint receiver_endpoint = *resolver.resolve(query);

    // Establish a socket for sending data
    udp::socket socket(io_service);
    // Initiate contact with the endpoint
    socket.open(udp::v4());

    // Establish a sending buffer
    boost::array<char, 1> send_buf  = {{ 0 }};

///This portion is modified from the reference
while(1)
{
      std::cin.read(send_buf.data(),1);
      if(send_buf[0]==' ')break;				//To escape
      if(send_buf[0]=='\n')continue;			//Do not add enter characters
///
      socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

    // Prepare to accept whatever server sends back
    boost::array<char, 128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len = socket.receive_from(
        boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout.write(recv_buf.data(), len);
}
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}
